package com.ruoyi.xuexiao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.constant.CsbsEnum;
import com.ruoyi.constant.DataConstant;
import com.ruoyi.xuexiao.domain.entity.BasicRegion;
import com.ruoyi.xuexiao.domain.entity.WtCztzV3;
import com.ruoyi.xuexiao.domain.param.SuperviseParam;
import com.ruoyi.xuexiao.mapper.BasicRegionMapper;
import com.ruoyi.xuexiao.mapper.WtCztzV3Mapper;
import com.ruoyi.xuexiao.service.WtCztzV3Service;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 问题处置台账表(WtCztzV3)shix
 *
 * @author 田江余
 * @since 2024-05-28 14:13:53
 */
@Service
@AllArgsConstructor
public class WtCztzV3ServiceImpl extends ServiceImpl<WtCztzV3Mapper, WtCztzV3> implements WtCztzV3Service {

    private final WtCztzV3Mapper wtCztzV3Mapper;

    private final BasicRegionMapper basicRegionMapper;

    /**
     * 问题处置台账表(WtCztzV3)
     *
     * @author 田江余
     * @since 2024-05-28 14:24
     */
    @Override
    public List<BasicRegion> getCountyRegionsByCityName(SuperviseParam supervise) {
        LambdaQueryWrapper<WtCztzV3> wrapper = new LambdaQueryWrapper<>();
        // 查询上下半年
        wrapper.eq(WtCztzV3::getNd, supervise.getNd());
        List<WtCztzV3> wtCztzV3s = wtCztzV3Mapper.selectList(wrapper);

        //进行去重操作
        if (supervise.getSj() == 1) {
            wtCztzV3s = wtCztzV3s.stream()
                    .collect(Collectors.collectingAndThen(
                            Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(WtCztzV3::getXkzh))),
                            ArrayList::new
                    ));
        }

        // 查询所有数据
        List<BasicRegion> basicRegionse = basicRegionMapper.selectList(null);
        // 过滤出所有市级行政区
        List<BasicRegion> cityRegions = basicRegionse.stream()
                .filter(r -> r.getLevel() == DataConstant.REGION_LEVEL_OF_CITY)
                .collect(Collectors.toList());
        // 计算每个市级行政区的总数量、已处理数量和未处理数量
        for (BasicRegion cityRegion : cityRegions) {
            long total = wtCztzV3s.stream()
                    .filter(p -> p.getSxzqdm().equals(cityRegion.getAdcd()))
                    .count();
            long disposedOf = wtCztzV3s.stream()
                    .filter(p -> p.getSxzqdm().equals(cityRegion.getAdcd()) && p.getCzzt() == 1)
                    .count();
            long notDisposedOf = wtCztzV3s.stream()
                    .filter(p -> p.getSxzqdm().equals(cityRegion.getAdcd()) && p.getCzzt() == 2)
                    .count();
            cityRegion.setZs(total);
            cityRegion.setDisposedOf(disposedOf);
            cityRegion.setNotDisposedOf(notDisposedOf);
            if (wtCztzV3s.isEmpty()) {
                cityRegion.setZs(0L);
                cityRegion.setDisposedOf(0L);
                cityRegion.setNotDisposedOf(0L);
            }
        }
        return cityRegions;
    }

    /**
     * 问题类型统计
     * @author 田江余
     * @param
     * @return
     */
    @Override
    public List<WtCztzV3> countyRegionsByCityName(SuperviseParam supervise) {
        LambdaQueryWrapper<WtCztzV3> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WtCztzV3::getNd, supervise.getNd());

        List<WtCztzV3> wtCztzV3s = wtCztzV3Mapper.selectList(wrapper);
        wtCztzV3s = wtCztzV3s.stream()
                .collect(Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(WtCztzV3::getSjcs))),
                        ArrayList::new
                ));
        for (WtCztzV3 wtCztzV3 : wtCztzV3s) {
            // 设置Zs字段为当前区域的数量
            wtCztzV3.setZs((long) wtCztzV3s.size());
            // 设置DisposedOf字段为已处理的数量
            wtCztzV3.setDisposedOf(wtCztzV3s.stream()
                    .filter(p -> p.getCzzt() == 1)
                    .count());
            // 设置NotDisposedOf字段为未处理的数量
            wtCztzV3.setNotDisposedOf(wtCztzV3s.stream()
                    .filter(p -> p.getCzzt() == 2)
                    .count());
            // 如果Sjcs字段为KYQ，则设置为KYQ的描述
            if (Objects.equals(wtCztzV3.getSjcs(), CsbsEnum.KYQ.getCode())) {
                wtCztzV3.setSjcs(CsbsEnum.KYQ.getDesc());
                // 如果Sjcs字段为KBC，则设置为KBC的描述
            } else if (Objects.equals(wtCztzV3.getSjcs(), CsbsEnum.KBC.getCode())) {
                wtCztzV3.setSjcs(CsbsEnum.KBC.getDesc());
            }
        }
        return wtCztzV3s;
    }
}